package com.d404.server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;

import guanqu.server.DownloadInfo;
import im.beans.Group;
import im.beans.Group_User;
import im.beans.User;
import manager.PersistentManager;

/**
 * 获取群组内用户
 * Servlet implementation class GetGroupUsers
 */
//@WebServlet("/GetGroupUsers")
public class GetGroupUsers extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetGroupUsers() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		
		String data = request.getParameter("data");
		Upload upload = JSON.parseObject(data,Upload.class);
		
		Download download = new Download();
		
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
//		Session session = sf.openSession();
		Session session = sf.getCurrentSession();
		
		String result = "";
		
		Transaction tx = session.beginTransaction();
		try
		{
//			Query q = session.createQuery("from Group_User gu where gu.group = ?");
//			Group gTmep = new Group();
//			gTmep.setId(Long.parseLong(upload.getGroupId()));
//			q.setParameter(0, gTmep);
//			download.setGus(q.list());
			
			Query q = session.createQuery("select gu.id,gu.groupID,gu.userID,u.account,u.realName,u2.account "
					+ "from Group_User as gu,User as u,User as u2 "
					+ "where gu.userID = u.id and gu.groupID = ? and u2.id = gu.ownerID");
			q.setParameter(0, Long.parseLong(upload.getGroupId()));
			
			List<Group_User> guser = new ArrayList<Group_User>();
			for(Object o:q.list())
			{
				Object[] colums = (Object[]) o;
				Group_User gu = new Group_User();
				gu.setId((long) colums[0]);
				gu.setGroupID((long) colums[1]);
				gu.setUserID((long) colums[2]);
				User tempUser = new User();
				tempUser.setId((long) colums[2]);
				tempUser.setAccount((String) colums[3]);
				tempUser.setRealName((String) colums[4]);
				gu.setUser(tempUser);
				User owner = new User();
				owner.setAccount((String) colums[5]);
				gu.setOwner(owner);
				guser.add(gu);
			}
			download.setGus(guser);
			
			tx.commit();
			download.setStatus(DownloadInfo.SUCCESS);
		}catch(Exception e)
		{
			download.setStatus(DownloadInfo.FAILED);
			tx.rollback();
			e.printStackTrace();
		}finally {
//			session.close();
		}
		
		result = JSON.toJSONString(download);
		System.out.println(result);
		response.getWriter().append(result);
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	// 上行数据
	public static class Upload
	{
		String groupId;

		public String getGroupId() {
			return groupId;
		}

		public void setGroupId(String groupId) {
			this.groupId = groupId;
		}
		
	}
	
	
	// 下行数据
	public static class Download
	{
		int status;
		List<Group_User> gus = new ArrayList<Group_User>();
		public int getStatus() {
			return status;
		}
		public void setStatus(int status) {
			this.status = status;
		}
		public List<Group_User> getGus() {
			return gus;
		}
		public void setGus(List<Group_User> gus) {
			this.gus = gus;
		}
		
		
	}
			

}
